﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using admin.cyt369.com.Models;
using Admin.CYT369.Com.WebBase;

namespace admin.cyt369.com.Controllers
{
    public partial class OrganizationController
    {
        /// <summary>
        /// 机构树
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetOrganizationTree()
        {
            var not_id = Request.Query.GetInteger("NOT_ID");
            var organizations = Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.GetOrganizations().ToArray();

            var list = MakeOrganizationTree(organizations, 0, not_id);
            if (list.Count > 0)
            {
                list[0].iconCls = "fa fa-sun-o";
            }

            return DefaultJson(list);
        }

        private List<admin.cyt369.com.Models.Orgs.OrganizationTree> MakeOrganizationTree(IEnumerable<Admin.CYT369.Com.DAL.Domains.Organization> organizations, int parentId, int? not_id) {
            var list = new List<admin.cyt369.com.Models.Orgs.OrganizationTree>();
            foreach (var org in organizations.Where(c=>c.ParentID == parentId))
            {
                if (not_id.HasValue)
                {
                    if (org.Id == not_id.Value)
                    {
                        continue;
                    }
                }
                var item = new admin.cyt369.com.Models.Orgs.OrganizationTree(org);
                item.children = MakeOrganizationTree(organizations, org.Id, not_id);
                item.state = "open";

                list.Add(item);
            }

            return list;
        }

        public IActionResult GetOrganizationByID(int id)
        {
            var item = Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.GetOrganizationByID(id);

            return DefaultJson(item);
        }

        [HttpPost]
        public IActionResult SaveOrganization([FromForm]Admin.CYT369.Com.DAL.Domains.Organization organization)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();

            if (organization != null && Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.SaveOrganization(organization))
            {
                resp.Result = true;
                resp.Msg = System.Text.Json.JsonSerializer.Serialize(organization);
            }
            else
            {
                resp.Msg = "保存失败";
            }

            return DefaultJson(resp);
        }

        [HttpDelete]
        public IActionResult DeleteOrganization(int id)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();
            try
            {
                if (Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.DeleteOrganization(id))
                {
                    resp.Result = true;
                }
                else
                {
                    resp.Msg = "删除失败";
                }
            }
            catch (Admin.CYT369.Com.Utility.Exceptions.DataExistException ex)
            {
                resp.Msg = ex.Message;
            }

            return DefaultJson(resp);
        }

    }
}